import db, {roleTypes} from '../../database/lowDB/connectDb.js';
import { autoIncreamentKey} from '../../utils/index.js';


class roleTypesModel {
    constructor() {
        this.tableName = 'roleTypes';
    }
    
    // 根据角色id返回完整角色信息
    async showDetailRoleInfo(roleCodeList){
        if (!roleCodeList || roleCodeList.length === 0) {
            return [];
        }
        return roleTypes.filter(role => roleCodeList.includes(role.roleCode));
    }

    // 查询某个用户可见的的角色信息
    async searchRoleList(userId) {
        return roleTypes.filter(item => !item.userId || item.userId === userId);
    }


    // 新增自定义角色信息  ————  每个用户独有
    async addRoleMessage({userId, roleName}) {
        // 查找当前用户下的该角色是否已经存在
        let existingRole = roleTypes.find(item => (!item.userId || item.userId === userId) && item.roleName === roleName);
        if (existingRole) {
            return false
        }
        let id = autoIncreamentKey(roleTypes, 'roleCode');
        let newRole = {
            roleCode: id,
            userId,
            roleName
        };
        roleTypes.unshift(newRole);
        db.write();
        return id
    }
}   

export default new roleTypesModel();